import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPainter, QColor
from PyQt5.QtCore import QPoint

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __iter__(self):
        return iter((self.x, self.y))
    def __repr__(self):
        return '[{},{}]'.format(self.x, self.y)

class Interpolation:
    def __init__(self):
        self.keys = []
        self.compute = None
    def __iter__(self):
        return iter(self.keys)
    def __call__(self, x):
        return self.compute(x)
    def __bool__(self):
        return self.compute!=None
    def addKey(self, x, y):
        self.keys.append(Point(x, y))
        self.keys.sort(key=lambda k: k.x)

class Display(QWidget):
    def __init__(self, interpo):
        super().__init__()
        self.resize(400, 400)
        self.setWindowTitle('Interpolation')
        self.interpo = interpo
    def mousePressEvent(self, e):
        self.interpo.addKey(e.x(), e.y())
        self.update()
    def paintEvent(self, e):
        painter = QPainter(self)
        if self.interpo:
            points = [QPoint(x, self.interpo(x)) for x in range(self.width()) if self.interpo.keys[0].x<=x<=self.interpo.keys[-1].x]
            painter.setBrush(QColor(0, 0, 0))
            painter.drawPolyline(*points)
        painter.setBrush(QColor(255, 0, 0))
        for x, y in self.interpo:
            painter.drawEllipse(QPoint(x, y), 2, 2)

def run(interpo):
    app = QApplication(sys.argv)
    dis = Display(interpo)
    dis.show()
    return sys.exit(app.exec_())